<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col
        :span="3"
        class="left"
      >
        <!-- <img src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png" alt="美团"> -->
      </el-col>
      <el-col :span="15" class="center">
        <div class="wrapper">
          <el-input v-model="search" @focus="onFocus" @blur="onBlur" @input="input" placeholder="搜索商家或地点" />
          <button class="el-button el-button--primary">
            <i class="el-icon-search" />
          </button>
          <dl v-if="isHotPlace" class="hotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item,idx) of $store.state.home.hotPlace.slice(0,5)" :key="idx">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
          <dl v-if="isSearchList" class="searchList">
            <dd v-for="(item,idx) of searchList" :key="idx">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
        </div>
        <p class="suggset">
          <a
            v-for="(item,idx) in $store.state.home.hotPlace.slice(0,5)"
            :key="idx"
            :href="'/products?keyword='+encodeURIComponent(item.name)"
          >
            {{ item.name }}
          </a>
        </p>
        <ul class="nav">
          <li>
            <nuxt-link to="/" class="takeout">
              菜单分类1
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="movie">
              菜单分类2
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="hotel">
              菜单分类3
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="apartment">
              菜单分类4
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="business">
              菜单分类5
            </nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col :span="6" class="right">
        <ul class="security">
          <li>
            <i class="refund" />
            <p class="txt">
              随时退
            </p>
          </li>
          <li>
            <i class="single" />
            <p class="txt">
              不满意免单
            </p>
          </li>
          <li>
            <i class="overdue" />
            <p class="txt">
              过期退
            </p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import _ from 'lodash'
export default {
  data () {
    return {
      search: '',
      isFocus: false,
      hotPlace: ['火锅', '烤鱼', '串串香'],
      searchList: []
    }
  },
  computed: {
    isHotPlace () {
      return this.isFocus && !this.search
    },
    isSearchList () {
      return this.isFocus && this.search
    }
  },
  methods: {
    onFocus () {
      this.isFocus = true
    },
    onBlur () {
      setTimeout(() => {
        this.isFocus = false
      }, 200)
    },
    input: _.debounce(async function () {
      const city = this.$store.state.geo.position.city.replace('市', '') // 查询条件不能带市
      this.searchList = []
      const { status, data: { top } } = await this.$axios.get('/search/top', {
        params: {
          input: this.search,
          city
        }
      })
      this.searchList = status === 200 ? top.slice(0, 10) : []
    }, 500)
  }
}
</script>
<style lang="scss">
@import '@/assets/css/public/layout.scss';
@import '@/assets/css/public/header/search.scss';
</style>
